package examples;

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glVertex2f;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class InputDemo {
	
	List<Box> shapes = new ArrayList<Box>(16);
	private boolean somethingIsSelected = true;
	@SuppressWarnings("unused")
	private volatile boolean randomColorCoooldown = false; 

	public InputDemo() {
		try{
			Display.setDisplayMode(new DisplayMode(640,480));
			Display.setTitle("Input Demo");
			Display.create();
		} catch(LWJGLException e){
			e.printStackTrace();			
		}
		
		shapes.add(new Box(15,15));
		shapes.add(new Box(100,150));
		
		//Initialization code OpenGL
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0, 640, 480, 0, 1, -1);
		glMatrixMode(GL_MODELVIEW);
		
		while(!Display.isCloseRequested()) {
			//Render
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			
			while(Keyboard.next()){
				if(Keyboard.getEventKey() == Keyboard.KEY_C && Keyboard.getEventKeyState())
				{
					shapes.add(new Box(15,15));
				}
			}
			
			if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
				Display.destroy();
				System.exit(0);
			}
			
			
			
			for( Box box : shapes ){
				
				if(Mouse.isButtonDown(0) && box.inBounds(Mouse.getX(), 480 - Mouse.getY()) && !somethingIsSelected){
					somethingIsSelected = true;
					box.selected = true;
					System.out.println("you clicked me");
				}
				if(Mouse.isButtonDown(2) && box.inBounds(Mouse.getX(), 480 - Mouse.getY()) && !somethingIsSelected){
					box.randomizeColors();
					randomColorCoooldown = true;
					new Thread(new Runnable() {
						
						@Override
						public void run() {
							try {
								Thread.sleep(500);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}finally{
								randomColorCoooldown = false;
							}
						}
					}).run();
				}
				if(Mouse.isButtonDown(1)){
					box.selected = false;
					somethingIsSelected = false;
				}
				
				if(box.selected){
					box.update(Mouse.getDX(), Mouse.getDY());
				}
				box.draw();
			}
			
			Display.update();
			Display.sync(60);
		}

		Display.destroy();
	}
	
	private static class  Box {
		
		public int x, y;
		public boolean selected = false;
		private float colorRed, colorBlue, colorGreen;
		
		Box(int x, int y) {
			this.x = x;
			this.y = y;
			
			Random randomGenerator = new Random();
			colorRed = randomGenerator.nextFloat();
			colorGreen = randomGenerator.nextFloat();
			colorBlue = randomGenerator.nextFloat();
		}
		
		boolean inBounds(int mousex, int mousey){
			
			if(mousex > x && mousex < x + 50 && mousey > y && mousey < y + 50)
				return true;
			else
				return false;
		}
		
		void update(int dx, int dy){
			x += dx;
			y -= dy;
		}
		
		void randomizeColors(){
			
			Random randomGenerator = new Random();
			colorRed = randomGenerator.nextFloat();
			colorGreen = randomGenerator.nextFloat();
			colorBlue = randomGenerator.nextFloat();
		}
		
		void draw(){
			
			glColor3f(colorRed, colorGreen, colorBlue);
			
			glBegin(GL_QUADS);
				glVertex2f(x, y);
				glVertex2f(x+50, y);
				glVertex2f(x+50, y+50);
				glVertex2f(x, y+50);
			glEnd();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		new InputDemo();

	}

}
